/*
* 文件：Button.cs
* 作者：王阳
* 时间：2019-5-20
* 描述：
*/
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace UnityX
{
    [DisallowMultipleComponent]
    [AddComponentMenu("UI/UnityX/ButtonEx")]
    public class ButtonEx : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
    {
        public Graphic[] graphics;

        public Color normalColor = new Color(1, 1, 1, 1);
        public Color highlightedColor = new Color(0.95f, 0.95f, 0.95f, 1);
        public Color pressedColor = new Color(0.9f, 0.9f, 0.9f, 1);

        public float normalScale = 1;
        public float highlightedScale = 1.05f;
        public float pressedScale = 0.95f;

        public float normalSpeed = 0.3f;
        public float highlightedSpeed = 0.3f;
        public float pressedSpeed = 1.0f;

        [SerializeField]
        private UnityEvent m_OnClick = new UnityEvent();
        public UnityEvent onClick
        {
            get { return m_OnClick; }
            set { m_OnClick = value; }
        }

        private float currentScale = 1;
        private Color currentColor = Color.white;

        private bool isDown = false;
        private bool isEnter = false;
        private int state = 0;

        private void Awake()
        {
            if (graphics == null || graphics.Length == 0)
            {
                //Graphic graphic = GetComponent<Graphic>();
                //if (graphic != null)
                //{
                //    graphics = new Graphic[] { graphic };
                //}
                //else
                //{
                //    graphics = new Graphic[0];
                //}
                graphics = GetComponentsInChildren<Graphic>();
            }
        }
        private void OnEnable()
        {
            ResetState();
        }
        private void OnDisable()
        {
            ResetState();
        }
        private void ResetState()
        {
            if (state == 0) return;

            state = 0;
            isDown = false;
            isEnter = false;
            for (int i = 0; i < graphics.Length; i++)
            {
                //graphics[i].color = normalColor * graphicColors[i];
                graphics[i].CrossFadeColor(normalColor, 0f, true, true);
            }
            transform.localScale = new Vector3(normalScale, normalScale, normalScale);
        }
        private void Update()
        {
            switch (state)
            {
                case 0:
                    break;
                case 1://进入过渡中
                    currentScale = Mathf.MoveTowards(currentScale, highlightedScale, Time.deltaTime * highlightedSpeed);
                    transform.localScale = new Vector3(currentScale, currentScale, currentScale);
                    break;
                case 2://进入
                    break;
                case 3://按下过渡中
                    currentScale = Mathf.MoveTowards(currentScale, pressedScale, Time.deltaTime * pressedSpeed);
                    transform.localScale = new Vector3(currentScale, currentScale, currentScale);
                    break;
                case 4://按下
                    break;
                case 5://退出过渡中
                    {
                        if (currentScale < normalScale)
                        {
                            currentScale += Time.deltaTime * normalSpeed;
                            if (currentScale > normalScale)
                            {
                                currentScale = normalScale;
                                state = 0;
                            }
                        }
                        else if (currentScale > normalScale)
                        {
                            currentScale -= Time.deltaTime * normalSpeed;
                            if (currentScale < normalScale)
                            {
                                currentScale = normalScale;
                                state = 0;
                            }
                        }
                        else
                        {
                            currentScale = normalScale;
                            state = 0;
                        }
                        transform.localScale = new Vector3(currentScale, currentScale, currentScale);
                    }
                    break;
                default:
                    break;
            }
        }
        public void OnPointerDown(PointerEventData eventData)
        {
            isDown = true;
            state = 3;
            for (int i = 0; i < graphics.Length; i++)
            {
                //graphics[i].color = pressedColor * graphicColors[i];
                graphics[i].CrossFadeColor(pressedColor, 0f, true, true);
            }
        }
        public void OnPointerUp(PointerEventData eventData)
        {
            isDown = false;
            if (isEnter)
            {
                state = 1;
                for (int i = 0; i < graphics.Length; i++)
                {
                    //graphics[i].color = highlightedColor * graphicColors[i];
                    graphics[i].CrossFadeColor(highlightedColor, 0f, true, true);
                }
            }
            else
            {
                state = 5;
                for (int i = 0; i < graphics.Length; i++)
                {
                    //graphics[i].color = normalColor * graphicColors[i];
                    graphics[i].CrossFadeColor(normalColor, 0f, true, true);
                }
            }
        }
        public void OnPointerEnter(PointerEventData eventData)
        {
            isEnter = true;
            if (isDown)
            {
            }
            else
            {
                state = 1;
                for (int i = 0; i < graphics.Length; i++)
                {
                    //graphics[i].color = highlightedColor * graphicColors[i];
                    graphics[i].CrossFadeColor(highlightedColor, 0f, true, true);
                }
            }
        }
        public void OnPointerExit(PointerEventData eventData)
        {
            isEnter = false;
            if (isDown)
            {
            }
            else
            {
                state = 5;
                for (int i = 0; i < graphics.Length; i++)
                {
                    //graphics[i].color = normalColor * graphicColors[i];
                    graphics[i].CrossFadeColor(normalColor, 0f, true, true);
                }
            }
        }
        public void OnPointerClick(PointerEventData eventData)
        {
            m_OnClick.Invoke();
        }
    }
}